package com.tedu.bbs.controller;

import com.tedu.bbs.util.DBUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

@Controller
public class UserController {

    @RequestMapping("/userList")
    public void userList(HttpServletRequest request,HttpServletResponse response){
        System.out.println("开始处理动态页面！！！");

    /*
    1:从数据库中将所有用户信息查询出来
    2：将每个用户信息显示在页面上
     */

        try(
                Connection connection = DBUtil.getConnection();
                ){
            String sql ="SELECT id,username,password,nickname,age FROM userinfo";
            PreparedStatement ps = connection.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();

            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw = response.getWriter();
            pw.println("<!DOCTYPE html>");
            pw.println("<html lang=\"en\">");
            pw.println("<head>");
            pw.println("    <meta charset=\"UTF-8\">");
            pw.println("    <title>用户列表</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("<center>");
            pw.println("    <h1>用户列表</h1>");
            pw.println("    <table border=\"1\">");
            pw.println("        <tr>");
            pw.println("            <td>ID</td>");
            pw.println("            <td>用户名</td>");
            pw.println("            <td>密码</td>");
            pw.println("            <td>昵称</td>");
            pw.println("            <td>年龄</td>");
            pw.println("        </tr>");
            while(rs.next()) {
                int id=rs.getInt("id");
                String username = rs.getString("username");
                pw.println("             <tr>");
                pw.println("                <td>"+id+"</td>");
                pw.println("                <td><a href='/articleList?userId="+id+"&username="+username+"'>" + rs.getString("username") + "</a></td>");
                pw.println("                <td>" + rs.getString("password") + "</td>");
                pw.println("                <td>" + rs.getString("nickname") + "</td>");
                pw.println("                <td>" + rs.getInt("age") + "</td>");
                pw.println("            </tr>");
            }
                pw.println("        </table");
                pw.println("    </center>");
                pw.println("</body>");
                pw.println("</html>");

        rs.close();
        ps.close();

        }catch (Exception e){
            e.printStackTrace();
        }

}

    @RequestMapping("/regUser")
    //public void reg(HttpServletRequest request, HttpServletResponse response) throws IOException {
    public void reg(String username,String password,String nickname,int age, HttpServletResponse response) throws IOException {
        /*String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String ageStr = request.getParameter("age");
        */if(username == null || username.isEmpty()||
           password == null || password.isEmpty()||
           nickname == null || nickname.isEmpty()
           /*ageStr == null || ageStr.isEmpty() ||
                (!ageStr.matches("[0-9]+"))*/){
            response.sendRedirect("/reg_info_error.html");
            return;
        }
        //int age = Integer.parseInt(ageStr);

        try(
                Connection connection = DBUtil.getConnection()
                ){
             String sql ="SELECT 1 " +
                  "FROM userinfo " +
                  "WHERE username=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,username);
            ResultSet rs = ps.executeQuery();
            if (rs.next()){
                response.sendRedirect("/have_user.html");
                return;
            }else{
                String sql1 ="INSERT INTO userinfo (username,password,nickname,age) VALUES (?,?,?,?)";
                PreparedStatement ps1 = connection.prepareStatement(sql1);
                ps1.setString(1,username);
                ps1.setString(2,password);
                ps1.setString(3,nickname);
                ps1.setInt(4,age);
                int num = ps1.executeUpdate();
                if(num>0){
                    response.sendRedirect("/reg_success.html");
                    return;
                }else{
                    response.sendRedirect("/reg.html");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        System.out.println("开始处理注册业务！！！");

    }

    @RequestMapping("/loginUser")
    public void login(HttpServletRequest resquest,HttpServletResponse response) throws IOException {
        System.out.println("开始处理登录!!!");
        String username = resquest.getParameter("username");
        String password = resquest.getParameter("password");
        if(username==null || username.isEmpty()||
           password==null || password.isEmpty()){
            response.sendRedirect("/login_info_error.html");
        }

        try(
                Connection connection = DBUtil.getConnection();
            ){
               String sql = "SELECT username,password " +
                            "FROM userinfo " +
                            "WHERE username=? " +
                            "AND password=?";
               PreparedStatement ps = connection.prepareStatement(sql);
               ps.setString(1,username);
               ps.setString(2,password);
               ResultSet rs = ps.executeQuery();
               if(rs.next()){
                   response.sendRedirect("/login_success.html");
               }else{
                   response.sendRedirect("/login_fail.html");
               }
        }
        catch (Exception e){
            e.printStackTrace();
        }


    }


}
